package com.pactera.miyuangroup.controller.web;

import com.pactera.miyuangroup.vo.convertor.RoleConvert;
import com.pactera.miyuangroup.vo.request.web.RoleRequest;
import com.pactera.miyuangroup.vo.response.ResultModel;
import com.pactera.miyuangroup.vo.response.web.RoleRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pactera.miyuangroup.db.entity.Role;
import com.pactera.miyuangroup.service.RoleService;
import com.pactera.miyuangroup.vo.response.VoidResponse;

import java.util.List;

/**
 * 角色controller
 *
 * @ClassName: RoleController
 * @Description:
 * @date 2018年4月17日 下午4:18:40
 */
@Api(description = "角色接口")
@RestController
@RequestMapping("role")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleConvert roleConvert;

    /**
     * 新增或更新角色
     *
     * @param @param  role
     * @param @return
     * @throws
     * @Description:
     * @date 2018年4月17日 下午4:18:29
     */
    @ApiOperation(value = "新增或更新角色", notes = "新增或更新角色")
    @RequestMapping(value = "addOrUpdateRole", method = RequestMethod.POST)
    public ResultModel<VoidResponse> addRole(@RequestBody RoleRequest role) {
        log.info("调用新增角色接口");
        Role r = new Role();
        if (StringUtils.isNotBlank(role.getId())) {
            r.setId(role.getId());
            log.info("修改角色的id{}", role.getId());
        }
        r.setRoleCode(role.getRoleCode().toUpperCase());
        r.setRoleName(role.getRoleName());
        boolean flg = roleService.insertOrUpdate(r);
        if (!flg) {
            ResultModel.fail();
        }
        return ResultModel.ok();
    }


    /**
     * 角色逻辑删除
     *
     * @param @param  roleId
     * @param @return
     * @throws
     * @Description:
     * @date 2018年4月17日 下午4:18:52
     */
    @ApiOperation(value = "删除角色", notes = "删除角色")
    @RequestMapping(value = "deleteRole/{roleId}", method = RequestMethod.GET)
    public ResultModel<VoidResponse> deleteRole(@PathVariable String roleId) {
        log.info("调用删除角色接口");
        log.info("删除角色的id{}", roleId);
        boolean flg = roleService.deleteById(roleId);
        if (!flg) {
            ResultModel.fail();
        }
        return ResultModel.ok();
    }


    /**
     * 查询角色
     *
     * @param @param  roleId
     * @param @return
     * @throws
     * @Description:
     * @date 2018年4月17日 下午4:18:52
     */
    @ApiOperation(value = "查询角色", notes = "查询角色明细")
    @RequestMapping(value = "getRole/{roleId}", method = RequestMethod.GET)
    public ResultModel<RoleRes> getRole(@PathVariable String roleId) {
        log.info("调用查询角色接口");
        log.info("查询角色的id{}", roleId);
        Role role = roleService.selectById(roleId);
        if (null == role) {
            ResultModel.fail();
        }
        RoleRes roleRes = roleConvert.convert2RoleRes(role);
        return ResultModel.ok(roleRes);
    }

    /**
     * 查询所有角色
     *
     * @param @return
     * @date 2018年05月11日10:19:34
     */
    @ApiOperation(value = "查询所有角色", notes = "查询所有角色")
    @RequestMapping(value = "getRoleList", method = RequestMethod.GET)
    public ResultModel<List<RoleRes>> getRoleList() {
        log.info("调用查询角色集合接口");
        List<Role> roles = roleService.selectList(null);
        List<RoleRes> roleRes = roleConvert.convert2RoleResList(roles);
        return ResultModel.ok(roleRes);
    }

}
